﻿Imports System.Xml

Public Class frmNewOrganization

    Const Max = 20
    Dim j As Integer = 0
    Dim textbox(Max) As TextBox
    Dim arrNode As New ArrayList
    Dim _xmlFunction As New XmlFunction()

    Private Sub LoadNodeXml()
        Dim xmldoc As New XmlDocument

        Dim Doc As New XmlDocument
        Dim file_name As String
        file_name = Application.StartupPath
        If file_name.EndsWith("\bin\Debug") Then
            file_name = file_name.Substring(0, file_name.LastIndexOf("\bin\Debug"))
        End If
        file_name &= "\Xml\XmlConfigSubjects.xml"

        xmldoc.Load(file_name)
        Dim node As XmlNode = xmldoc.SelectSingleNode("/RootSubject/Subject[@type = 'Organization']")
        Dim i As Integer = 10
        For Each n As XmlNode In node.ChildNodes
            Dim lble As Label = New Label
            arrNode.Add(n.Name)
            textbox(j) = New TextBox
            With lble
                .ForeColor = Color.Blue

                .Location = New Point(1, i)
                .Text = n.Name
                .TextAlign = ContentAlignment.BottomRight
                .Show()
            End With
            With textbox(j)
                .Name = "txt"
                .Location = New Point(110, i + 5)
                .Size = New Size(300, 25)
                .Anchor = AnchorStyles.Right + AnchorStyles.Left + AnchorStyles.Top

                .Show()
            End With
            Me.Controls.Add(lble)
            Me.Controls.Add(textbox(j))
            i = i + 27
            j = j + 1
        Next
        btnAdd.Location = New Point(200, i + 5)
        btnCancle.Location = New Point(300, i + 5)
        dtgOrganization.Location = New Point(10, i + 10 + btnAdd.Height)
        'gVStory.Location = New Point(10, i + 10 + btnAdd.Height)
        dtgOrganization.Height = 200
        btnSave.Location = New Point(400, i + 20 + btnAdd.Height + dtgOrganization.Height)
        Me.Height = i + 90 + btnAdd.Height + dtgOrganization.Height
        'gVStory.Anchor = AnchorStyles.Right + AnchorStyles.Left + AnchorStyles.Top + AnchorStyles.Bottom

    End Sub

    Private Sub frmNewOrganization_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadNodeXml()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        'bắt null
        Dim i As Integer
        Dim k As Integer = 0
        For i = 0 To j - 1
            If textbox(i).Text = "" Then
                k = k + 1
            End If
        Next
        If k = j Then
            MessageBox.Show("Please enter data")
        Else
            Dim Doc As New XmlDocument
            Dim file_name As String
            file_name = Application.StartupPath
            If file_name.EndsWith("\bin\Debug") Then
                file_name = file_name.Substring(0, file_name.LastIndexOf("\bin\Debug"))
            End If
            file_name &= "\Xml\XmlOrganization.xml"

            Doc.Load(file_name)
            ' Tạo một phần tử mới.
            Dim ItemNode As XmlNode
            ItemNode = Doc.CreateElement("Organazation")
            ' Thêm đặc tính.
            Dim Attribute As XmlAttribute
            Attribute = Doc.CreateAttribute("id")
            Attribute.Value = _xmlFunction.GetRecord(file_name) + 1
            ItemNode.Attributes.Append(Attribute)
            ' Tạo và thêm các phần tử con cho nút này.
            Dim Node(Max) As XmlNode
            For i = 0 To j - 1
                Node(i) = Doc.CreateElement(arrNode(i).ToString)
                ItemNode.AppendChild(Node(i))
                Node(i).AppendChild(Doc.CreateTextNode(Trim(textbox(i).Text)))
                'MsgBox(textbox(i).Text)
            Next
            ' Thêm phần tử mới. Trong trường hợp này, chúng ta
            ' cho nó làm con ở cuối danh sách item.
            Doc.DocumentElement.AppendChild(ItemNode)
            ' Lưu tài liệu.
            Doc.Save(file_name)
            MsgBox("Add successful")
            Me.Close()
        End If
    End Sub
End Class